<template>
	<view>
		<diy-template ref="newDiy" :scrollTop="scrollTop" v-if="storeList.length>0"></diy-template>
	</view>
</template>
<script>
	var app = getApp().globalData
	import diyTemplate from "@/components/common/diy_template";

	export default {
		data(){
			return {
				"template_type":0, // 0是未获取 1是默认页面 2是版本装修页面 3是新版装修页面
				"storeList": [], // 门店列表
				"phone":"" ,// 手机号
				"storeName":"" ,// 门店名称	
				"newPeople": false,//新手引导
				"store_switch": 1,
				"cityCode":"", // 支付宝用城市编码
				"init_state":0,// 页面初始构建状态
				"store_id":0, // 当前进入的门店ID
				"page_id":"", // 装修的id
				"scrollTop":0,// 页面滚动高度
				"page_content":"", // 页面的装修数据
			}
		},
		 components: {
			diyTemplate
		 },
		 async onShow() {
		 	getApp().globalData.resetStoreId();
		 	if (!this.storeList.length || this.storeList[0].store_id != this.$store.store_id) {
		 		await this.getStoreList();
		 	}
			await this.getPageData();
		 	if (!this.storeList.length) return;
			// 要触发插件里面的onshows方法
			this.$nextTick(() => {
				 this.$refs["newDiy"].onShows();
			 })
		 },
		 methods:{
			 async getPageData() {
			 	try {
			 		const {
			 			status,
			 			data,
			 			message,
			 		} = await this.$request({
			 			url: '/DiyStore/Api/getPageData'
			 		},{
			 			needErrorMsg:false,
						page_id:this.page_id
			 		});
			 		if (status == 1) {
			 			this.page_content = data.content
			 			return true;
			 		} else {
			 			return false;
			 		}
			 	} catch (err) {
			 		return false;
			 	}
			 },
			// 获取所有的门店数据
			getStoreList(){
				return new Promise((resolve, reject) => {
					app.getStoreList(false, this.$store.begin_store_id ? this.$store.begin_store_id : '').then((res) => {
						if (res.store.length && res.store.length > 0) {
							this.$store.store_id = res.store[0].store_id;
							this.setData({
								store_id: res.store[0].store_id,
								storeList: res.store,
								phone: res.store[0].phone,
								storeName: res.store[0].store_name,
								store_switch: res.store_switch,
								// #ifdef MP-ALIPAY
								cityCode: res.store[0].cityCode,
								// #endif
							});
							uni.setStorageSync('storeInfo', res.store[0]);
									
							let newPeopleStorage = uni.getStorageSync('newPeopleStorage');
									
							if (!newPeopleStorage) {
								this.setData({
									newPeople: true
								});
								uni.setStorageSync('newPeopleStorage','true');
							}
							resolve(true)
						} else {
							uni.showToast({
								title: '未查找到门店',
								icon: 'none',
								duration: 2000,
								mask: true
							});
							reject(false)
						}
					}).catch(() => {
						reject(false)
					})
				})
			},
		},
		 /**
		  * 上拉刷新加载
		  */
		 async onPullDownRefresh(){
			 if (!this.storeList.length || this.storeList[0].store_id != this.$store.store_id) {
			 	await this.getStoreList();
			 }
			 this.$nextTick(() => {
				 this.$refs["newDiy"].onPagePullDownRefresh();
			  })
			  uni.stopPullDownRefresh();
		 },
		 /**
		  * 调用组件的下拉加载
		  */
		 onReachBottom(){
			 if(typeof(this.$refs["newDiy"].onPageReachBottom)=="function"){
				this.$refs["newDiy"].onPageReachBottom();
			  }
		 },
		 onShareAppMessage() {
		 	return {
		 		title: this.storeList[0] ? this.storeList[0].store_name : this.store_name,
		 		path: '/pages/store/diy/diy?store_id=' + this.$store.store_id + '&share_uid=' + app.XcxData.userInfo.uid,
		 	};
		 },
		 async onLoad(e) {
			if(e.page){
				this.page_id = e.page;
			}
			if(e.store_id){
				this.store_id = e.store_id
				this.$store.begin_store_id = e.store_id
			}
			await this.$onLaunched;
			 if (!this.storeList.length || this.storeList[0].store_id != this.$store.store_id) {
			 	await this.getStoreList();
			 }
			 this.init_state = 1;
				uni.showShareMenu({
					menus: ['shareAppMessage', 'shareTimeline']
				});
			
		 },
		 onPageScroll(e) {
		 	// 传入scrollTop值并触发所有easy-loadimage组件下的滚动监听事件
		 	this.scrollTop = e.scrollTop;
		 			
		 },
		 onShareTimeline(res) {
		 	return {
		 		title: this.storeList[0].store_name,
		 		query: 'store_id=' + this.storeList[0].store_id
		 	}
		 }
	}
</script>
<style>
	@import '@/pages/store/components/diy-goods/diy-goods.css';
</style>
<style>
	.uni-navbar__content{
		z-index: 100000 !important;
	}
</style>
